package com.soft.mainsite.module.web.system.service.impl;

import com.soft.mainsite.common.utils.CmsUtil;
import com.soft.mainsite.common.utils.HtmlKit;
import com.soft.mainsite.common.utils.PinyinUtil;
import com.soft.mainsite.mybatis.mapper.TSysPermissionMapper;
import com.soft.mainsite.mybatis.model.TSysPermission;
import com.soft.mainsite.mybatis.model.TSysRole;
import com.soft.mainsite.mybatis.model.TSysRolePermission;
import com.soft.mainsite.common.utils.JsonUtil;
import com.soft.mainsite.module.web.system.service.RoleService;
import com.soft.mainsite.mybatis.mapper.TSysRoleMapper;
import com.soft.mainsite.mybatis.mapper.TSysRolePermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Description:角色service
 *
 * @author Jin
 * @create 2017-04-14
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private TSysRoleMapper roleMapper;

    @Autowired
    private TSysPermissionMapper permissionMapper;

    @Autowired
    private TSysRolePermissionMapper rolePermissionMapper;


    @Override
    public TSysRole findByid(Integer id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TSysRole> findByUserId(Integer userId) {
        return roleMapper.selectRoleByUserId(userId);
    }

    @Override
    public String delete(Integer id) {

        if (roleMapper.deleteByPrimaryKey(id) > 0) {
            rolePermissionMapper.delectByRoleId(id);
            return JsonUtil.toSuccess("删除成功！", "role", true);
        }
        return JsonUtil.toError("删除失败！");
    }

    @Override
    @Transactional(transactionManager = "masterTransactionManager", rollbackFor = Exception.class)
    public String update(TSysRole role, Integer[] permissionId) {
        role.setRolename(PinyinUtil.convertLower(HtmlKit.getText(role.getRolename()).trim()));
        if (roleMapper.updateByPrimaryKey(role) > 0) {
            if (!CmsUtil.isNullOrEmpty(permissionId)) {
                // 清空当前角色的权限
                rolePermissionMapper.delectByRoleId(role.getRoleId());
            }
            if (!CmsUtil.isNullOrEmpty(permissionId) && permissionId.length > 0) {
                for (int id : permissionId) {
                    TSysRolePermission rp = new TSysRolePermission();
                    rp.setRoleId(role.getRoleId());
                    rp.setPermissonId(id);
                    saveRolePermission(rp);
                }
            }
            return JsonUtil.toSuccess("操作成功！", "role", true);
        }

        return JsonUtil.toError("操作失败！");
    }

    @Override
    @Transactional(transactionManager = "masterTransactionManager", rollbackFor = Exception.class)
    public String save(TSysRole role, Integer[] permissionId) {
        if (role != null) {
            role.setRolename(PinyinUtil.convertLower(HtmlKit.getText(role.getRolename()).trim()));
            if (roleMapper.insert(role) > 0) {
                if (permissionId != null && permissionId.length > 0) {
                    // 遍历添加角色权限
                    for (int id : permissionId) {
                        TSysRolePermission rp = new TSysRolePermission();
                        rp.setPermissonId(id);
                        rp.setRoleId(role.getRoleId());
                        saveRolePermission(rp);
                    }
                }
                return JsonUtil.toSuccess("操作成功", "role", true);
            }
        }
        return JsonUtil.toError("操作失败！");
    }

    @Override
    public List<TSysRole> findAll() {
        return roleMapper.selectAll();
    }

    @Override
    public List<TSysPermission> findPermissonByPid(Integer pid) {
        return permissionMapper.selectByPid(pid);
    }

    @Override
    public Integer findPermissionCountByRoleId(Integer roleId, Integer permissionId) {
        return rolePermissionMapper.selectCountByRoleIdAndPermissionId(roleId, permissionId);
    }

    @Override
    public Integer saveRolePermission(TSysRolePermission rolePermission) {
        return rolePermissionMapper.insert(rolePermission);
    }

    @Override
    public TSysPermission findPermissonByid(Integer pid) {
        return permissionMapper.selectByPrimaryKey(pid);
    }

    @Override
    public String save(TSysPermission permission) {
        permission.setName(PinyinUtil.convertLower(HtmlKit.getText(permission.getName())));
        if (permissionMapper.insert(permission) > 0) {
            return JsonUtil.toSuccess("权限增加成功", "permission", true);
        }
        return JsonUtil.toError("权限增加失败");
    }

    @Override
    public String update(TSysPermission permission) {
        permission.setName(PinyinUtil.convertLower(HtmlKit.getText(permission.getName())));
        if (permissionMapper.selectByPrimaryKey(permission.getPermissionId()).getPid() == 0 && permission.getPid() != 0) {
            // 父类交换
            TSysPermission p = permissionMapper.selectByPrimaryKey(permission.getPid());
            p.setPid(0);
            permission.setPid(permission.getPid());
            permissionMapper.updateByPrimaryKey(p);
        }
        if (permissionMapper.updateByPrimaryKey(permission) > 0) {
            return JsonUtil.toSuccess("权限更新成功", "permission", true);
        }
        return JsonUtil.toError("权限更新失败");
    }

    @Override
    @Transactional(transactionManager = "masterTransactionManager", rollbackFor = Exception.class)
    public String delectPermissionById(Integer id) {

        if (permissionMapper.deleteByPrimaryKey(id) > 0) {
            // 删除角色权限
            rolePermissionMapper.delectByPermissionId(id);
            // 删除子节点的角色关联
            List<TSysPermission> permissions = permissionMapper.selectByPid(id);
            if (permissions != null && permissions.size() > 0) {
                for (TSysPermission p : permissions) {
                    rolePermissionMapper.delectByPermissionId(p.getPermissionId());
                }
            }
            // 删除子节点
            permissionMapper.deleteByPid(id);
            return JsonUtil.toSuccess("权限删除成功", "permission", true);
        }

        return JsonUtil.toError("权限删除失败");
    }


}
